﻿using Microsoft.AspNetCore.Mvc;
using Stu_sys.Entity;
using Stu_sys.Models;
using Stu_sys.utils;
using System.Text.Json;

namespace Stu_sys.Controllers
{
    public class OfficeRoomController : Controller
    {
        private readonly MysqlContext _mysqlContext;
        public OfficeRoomController(MysqlContext mysqlContext) 
        { 
            _mysqlContext = mysqlContext;
        }
        public IActionResult OfficeRoomList()
        {
            return View();
        }

        [HttpGet]
        public IActionResult GetOfficeRoomList() 
        {
            var attrStr = MyUtils.GetAttributeToJsonStr(typeof(OfficeRoomViewModel));
            var officList = _mysqlContext.OfficeRooms.ToList();
            var officeStr = JsonSerializer.Serialize(officList);
            var data = new
            {
                attr = attrStr,
                row = officeStr
            };
            return Json((data));
        }

        //AddOfficeRoom
        [HttpPost]
        public IActionResult AddOfficeRoom(OfficeRoom office)
        {
            if (office.OfficeRoomName == "")
            {
                return Json(-1);
            }

            if (IsExistOfficeRoom(office))
            {
                return Json(0);
            }
            _mysqlContext.OfficeRooms.Add(office);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }


        [HttpPost]
        public IActionResult EditOfficeRoom(OfficeRoom officeRoom)
        {
            
            if (officeRoom.OfficeRoomName == "" || officeRoom.Capacity <0)
            {
                return Json(-1);
            }

            var office = _mysqlContext.OfficeRooms.Where(d => d.OfficeRoomId == officeRoom.OfficeRoomId).FirstOrDefault();
            office.OfficeRoomName=officeRoom.OfficeRoomName;
            office.Capacity = officeRoom.Capacity;
            office.Description = officeRoom.Description;

            _mysqlContext.OfficeRooms.Update(office);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }

        [HttpPost]
        public IActionResult DelOfficeRoom(OfficeRoom officeRoom)
        {
            if (officeRoom == null)
            {
                return Json(-1);
            }
            var d = _mysqlContext.OfficeRooms.Single(d => d.OfficeRoomId == officeRoom.OfficeRoomId);
            _mysqlContext.Remove(d);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }


        /// <summary>
        /// 办公室是否已存在
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        private bool IsExistOfficeRoom(OfficeRoom office)
        {
            var grades = _mysqlContext.OfficeRooms.Where(o=>o.OfficeRoomName==office.OfficeRoomName).ToList();
            if (grades.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
